﻿Imports Lucene.Net.Index
Imports Lucene.Net.Search
Imports Aricie.DNN.Settings
Imports Aricie.DNN.Modules.LuceneSearch.Settings
Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports DotNetNuke.Services.Localization


Namespace Aricie.DNN.Modules.LuceneSearch.UI

    Partial Public Class KeywordsFilter
        Inherits FieldFilterBase


#Region "Public Properties"

        Public Property TermOccur() As String
           

        Public Property Label() As String
            Get
                Return lblFilter.Text
            End Get
            Set(ByVal value As String)
                lblFilter.Text = value
            End Set
        End Property

        Public Property FuzzyValue() As String



        Public Function GetFuzzyValue() As Single
            Dim intFuzzy As Integer = 100
            If Me._FuzzyValue = "" Then
                Dim mySettings As LuceneResultsSettings = GetModuleSettings(Of LuceneResultsSettings)(SettingsScope.ModuleSettings, Me.ModuleID)
                intFuzzy = mySettings.DefaultFuzzyValue
            Else
                intFuzzy = Integer.Parse(Me._FuzzyValue)
            End If
            Return Convert.ToSingle(intFuzzy / 100)
        End Function


#End Region



        Public Overrides Function GetFilter() As LuceneFilterQuery
            Dim toReturn As New LuceneFilterQuery

            If Me.QueryMode = UI.QueryMode.Filter Then
                Dim keywordsFilter As New BooleanQuery
                Dim fieldValues As String() = FieldValue.Split(" "c)
                Dim objTermOccur As Occur = Occur.SHOULD

                Select Case TermOccur
                    Case "MUST"
                        objTermOccur = Occur.MUST
                    Case "MUST_NOT"
                        objTermOccur = Occur.MUST_NOT
                    Case "SHOULD"
                        objTermOccur = Occur.SHOULD
                End Select

                For Each strTerm As String In fieldValues
                    For Each fName As String In GetFieldNames()
                        Dim CurrentFuzzyValue As Single = GetFuzzyValue()
                        Dim fieldTermQuery As New FuzzyQuery(New Term(fName, strTerm), Convert.ToSingle(IIf(CurrentFuzzyValue = 1.0, 0.99, CurrentFuzzyValue)))
                        keywordsFilter.Add(fieldTermQuery, objTermOccur)
                    Next
                Next

                toReturn.FilterQuery = keywordsFilter
            Else
                toReturn.SearchQuery = FieldValue
            End If

            Return toReturn
        End Function

#Region "Validation du filtre"

        Private Function IsBooleanQueryValid(q As BooleanQuery) As Boolean
            For Each c As BooleanClause In q.GetClauses()
                If Not TypeOf c.Query() Is FuzzyQuery Then
                    Return False
                End If
            Next
            Return True
        End Function

        Private Function CheckMatchingQuery(q As Query) As Boolean
            Dim Filter = Filtering.FluentFilterRulesManagerFactory.Init() _
                         .Expects(Of FuzzyQuery)("FuzzyQuery unique") _
                         .ExpectsComplex("Plusieurs FuzzyQuery", AddressOf IsBooleanQueryValid) _
                         .Create()
            Return Filter.Matches(q)
        End Function

#End Region


        Public Overrides Function ParseFilter(ByVal objFilter As LuceneFilterQuery) As Boolean
            If Me.QueryMode = UI.QueryMode.Filter AndAlso CheckMatchingQuery(objFilter.FilterQuery) Then

                Dim objTerm As Term = BusinessController.ExtractAllTermsGetFirst(objFilter.FilterQuery)
                Dim goodField As Boolean = False
                For Each item As String In GetFieldNames()
                    If objTerm.Field = item Then
                        goodField = True
                        Exit For
                    End If
                Next
                If goodField Then
                    If txtKeywords.Text = "" Then
                        txtKeywords.Text = BusinessController.GetTermText(objTerm)
                    Else
                        txtKeywords.Text += " " + BusinessController.GetTermText(objTerm)
                    End If
                End If
                Return True
            ElseIf Me.QueryMode = UI.QueryMode.Search Then
                txtKeywords.Text = objFilter.SearchQuery
            End If
            Return False
        End Function


        Protected Overrides Property FieldValue() As String
            Get
                Return Me.txtKeywords.Text.ToLower
            End Get
            Set(ByVal value As String)
                Me.txtKeywords.Text = value
            End Set
        End Property


        Public Overrides Property Enabled() As Boolean
            Get
                Return Me.txtKeywords.Enabled
            End Get
            Set(ByVal value As Boolean)
                Me.txtKeywords.Enabled = value
            End Set
        End Property

        Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

            If Me.Label = "None" Then
                Me.lblFilter.Visible = False
            ElseIf Me.Label = String.Empty Then
                Me.Label = Localization.GetString("lblFilter", Me.LocalResourceFile)
            End If
        End Sub


     
    End Class

End Namespace